//
//  PushtaskField.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/1.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "PushtaskField.h"

@interface PushtaskField()
{
    CGFloat _selfHeight;
}

@end

@implementation PushtaskField

-(instancetype)initTaskFieldWithStyle:(TaskFieldStyle)style
{
    self = [super init];
    if (self) {
        
        [self instanceStyle:style];
        
    }
    return self;
}

- (void)instanceStyle:(TaskFieldStyle)style
{
    self.layer.masksToBounds = YES;
    self.layer.borderColor = [[UIColor blackColor] CGColor];
    self.layer.borderWidth = 1.0;
    
    _selfHeight = ((SCREEN_HEIGHT - 110) / 2 - 15) / 4;
    
    _field = [UITextField new];
    _field.font = [UIFont systemFontOfSize:15.0f];

    [self addSubview:_field];
    
    switch (style) {
        case TaskFieldStyleOfNormal:
        {
            [self makeNormalField];
        }
            break;
            
        case TaskFieldStyleOfSelect:
        {
            [self makeSelectField];
        }
            break;
            
        default:
            break;
    }
}

- (void)setText:(NSString *)text
{
    _text = text;
    _field.text = _text;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    _field.placeholder = _placeholder;
}

- (void)makeNormalField
{
    [_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];
}

- (void)makeSelectField
{
    _field.userInteractionEnabled = NO;
    
    UIView *line = [UIView new];
    line.backgroundColor = [UIColor blackColor];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.bottom.mas_equalTo(-8);
        make.right.mas_equalTo(-_selfHeight);
        make.width.mas_equalTo(1);
    }];
    
    UIImageView *selectImame = [UIImageView new];
    selectImame.image = [UIImage imageNamed:@"task_select_image"];
    [self addSubview:selectImame];
    [selectImame mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.bottom.mas_equalTo(-8);
        make.left.mas_equalTo(line.mas_right).mas_offset(8);
        make.width.mas_equalTo(_selfHeight - 16);
    }];
    
    
    [_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(line.mas_left).mas_equalTo(-5);
    }];
    
    UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc]init];
    [singlTap addTarget:self action:@selector(handlePanFrom:)];
    [self addGestureRecognizer:singlTap];

}

- (void)handlePanFrom:(UITapGestureRecognizer *)singlTap
{
    if ([self.delegate respondsToSelector:@selector(clickField:)]) {
        [self.delegate clickField:self];
    }
}

@end
